/**
 * IndexCtrl.java   2017年4月14日 下午4:46:27 by xuebing 
 *
 * Copyright (c) 2010 - 2017 All rights reserved.
 * 
 */
package com.rongmai.web.ctrl;

import com.rongmai.db.dto.CompanyDTO;
import com.rongmai.db.enums.UserRoleEnum;
import com.rongmai.db.model.BaseResult;
import com.rongmai.db.vo.CompanyVo;
import com.rongmai.db.vo.Page;
import com.rongmai.web.bo.CompanyBo;
import com.rongmai.web.bo.CompanyFutureBo;
import com.rongmai.web.bo.feign.CompanyFeignBo;
import com.rongmai.web.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/company")
//@Api(tags = "公司相关接口")
public class CompanyCtrl {

    @Resource
    private CompanyBo companyBo;

    @Resource
    private CompanyFeignBo companyFeignBo;
    @Resource
    private CompanyFutureBo companyFutureBo;

    @RequestMapping(value="/vague_query", method = RequestMethod.GET)
    public BaseResult<List<CompanyVo>> vagueQuery(CompanyVo companyVo){
        try {
            return BaseResult.succ(companyFeignBo.vagueQuery(companyVo));
        } catch (Exception e) {
            log.error("搜索异常", e);
        }
        return BaseResult.FAIL;
    }

    @RequestMapping("getFeature")
    public BaseResult getFeature(String role) {
        if (UserRoleEnum.FINANCE.name().equals(role) || UserRoleEnum.INVEST.name().equals(role)) {
            return BaseResult.succ(CompanyFutureBo.getTRFeature());
        }
        if (UserRoleEnum.SHAREHOLD_FINANCE.name().equals(role) || UserRoleEnum.SHAREHOLD_INVEST.name().equals(role)) {
            return BaseResult.succ(CompanyFutureBo.getGQTRFeature());
        }
        return BaseResult.SUCCESS;
    }

    @RequestMapping(value="/detail", method = RequestMethod.GET)
    public BaseResult<CompanyDTO> detail(Integer id){
        try {
            return BaseResult.succ(companyBo.detail(id));
        } catch (Exception e) {
            log.error("搜索异常", e);
        }
        return BaseResult.FAIL;
    }

    @RequestMapping(value="/my_company", method = RequestMethod.GET)
    public BaseResult<CompanyDTO> myCompany(Integer userId, HttpServletRequest request){
        try {
            return BaseResult.succ(companyBo.myCompany(userId));
        } catch (Exception e) {
            log.error("搜索异常", e);
        }
        return BaseResult.FAIL;
    }

    @RequestMapping(value="/edit", method = RequestMethod.POST)
    public BaseResult edit(@RequestBody CompanyDTO companyDTO, HttpServletRequest request){
        try {
            companyBo.edit(companyDTO,UserUtils.getCurrentUser(request));
            return BaseResult.succ();
        } catch (Exception e) {
            log.error("搜索异常", e);
        }
        return BaseResult.FAIL;
    }

    @RequestMapping(value="/query", method = RequestMethod.POST)
    public BaseResult<Page<CompanyDTO>> pageQuery(@RequestBody CompanyDTO companyDTO){
        try {
            return BaseResult.succ(companyBo.pageQuery(companyDTO));
        } catch (Exception e) {
            log.error("搜索异常", e);
        }
        return BaseResult.FAIL;
    }

//    @RequestMapping(value="/query/{code}", method = RequestMethod.POST)
//    public BaseResult<Page<CompanyDTO>> pageQuery(@RequestBody CompanyDTO companyDTO, @PathVariable String code){
//        try {
//            return BaseResult.succ(companyBo.pageQuery(companyDTO, code));
//        } catch (Exception e) {
//            log.error("搜索异常", e);
//        }
//        return BaseResult.FAIL;
//    }

}
